import { Component, OnInit } from '@angular/core';
import { Location as Location1 } from '@angular/common';
import { ActivatedRoute } from '@angular/router';
import { PostService } from 'src/app/service/post.service';
import { ConfirmationService, MessageService } from 'primeng/api';
import { ApiMenuService } from 'src/app/service/menu.service';

@Component({
    selector: 'app-permission-manage',
    templateUrl: './permission-manage.component.html',
    styleUrls: ['./permission-manage.component.scss'],
    providers: [ConfirmationService, MessageService],
})
export class PermissionManageComponent implements OnInit {
    id: any;
    tableData: any[];
    currentItem: any;
    hasChange = false;
    constructor(
        private location: Location1,
        private route: ActivatedRoute,
        private postService: PostService,
        private menuService: ApiMenuService,
        private confirmationService: ConfirmationService,
        private messageService: MessageService
    ) {
        route.queryParams.subscribe((queryParams) => {
            this.id = queryParams.id;
            console.log(this.id);
        });
    }
    async search() {
        const r = await this.menuService.fetchMenuListById({ id: this.id });
        const genChildList = (list) => {
            const res = [];
            list.forEach((item) => {
                if (!item.pid) {
                    res.push(item);
                }
            });
            list.forEach((item) => {
                if (item.pid) {
                    const it = res.find((it) => it.id === item.pid);
                    if (it) {
                        it.children = (it.children || []).concat(item);
                    }
                }
            });
            console.log('res', res);
            return res;
        };
        const genChildListUpdate = (list) => {
            const res = [];
            list.forEach((item) => {
                if (!item.pid) {
                    let temp = { data: null, children: [], expanded: true };
                    temp.data = item;
                    res.push(temp);
                }
            });
            list.forEach((item) => {
                if (item.pid) {
                    const it = res.find((it) => it.data.id === item.pid);
                    if (it) {
                        let temp = { data: null };
                        temp.data = item;
                        it.children = (it.children || []).concat(temp);
                    }
                }
            });
            return res;
        };

        this.tableData = genChildListUpdate(r || []);
        const it = await this.postService.fetchPostById({ id: this.id });
        this.currentItem = it || {};
        //console.log(this.currentItem)
    }
    ngOnInit(): void {
        this.search();
    }
    goBack() {
        this.location.back();
    }
    selectAllByObj(o) {
        const r = this.tableData;
        for (let i = 0; i < r.length; i++) {
            const it = r[i];
            it.data.roleId = o;
            if (it.children) {
                for (let j = 0; j < it.children.length; j++) {
                    const c = it.children[j];
                    c.data.roleId = o;
                }
            }
        }
        this.hasChange = true;
    }
    selectRow(item) {
        const r = this.tableData;
        let current;
        for (let i = 0; i < r.length; i++) {
            const it = r[i];
            if (it.data.id === item.id) {
                current = it;
                break;
            }
            if (it.children) {
                for (let j = 0; j < it.children.length; j++) {
                    const c = it.children[j];
                    if (c.data.id === item.id) {
                        current = c;
                        break;
                    }
                }
            }
            if (current) {
                break;
            }
        }
        if (current) {
            current.data.roleId = this.id;
            this.hasChange = true;
        }
    }
    unSelectRow(item) {
        const r = this.tableData;
        let current;
        for (let i = 0; i < r.length; i++) {
            const it = r[i];
            if (it.data.id === item.id) {
                current = it;
                break;
            }
            if (it.children) {
                for (let j = 0; j < it.children.length; j++) {
                    const c = it.children[j];
                    if (c.data.id === item.id) {
                        current = c;
                        break;
                    }
                }
            }
            if (current) {
                break;
            }
        }
        if (current) {
            current.data.roleId = null;
            this.hasChange = true;
        }
    }
    selectAll() {
        this.selectAllByObj(this.id);
    }
    selectNone() {
        this.selectAllByObj(null);
    }
    reset = async () => {
        await this.search();
        this.hasChange = false;
    };
    async save() {
        const genSaveList = (list) => {
            const res = [];
            list.forEach((item) => {
                res.push({
                    id: item.data.id + '',
                    roleId: item.data.roleId,
                });
                item.children &&
                    item.children.forEach((it) => {
                        res.push({
                            id: it.data.id + '',
                            roleId: it.data.roleId,
                        });
                    });
            });
            return res.filter((item) => item.roleId);
        };
        const res = await this.menuService.saveMenuByList({
            id: this.id,
            list: genSaveList(this.tableData),
        });
        if (res.code === '200') {
            this.messageService.add({
                severity: 'info',
                summary: '确认',
                detail: 'API权限已经修改',
            });
            await this.reset();
        }
    }
}
